//index.js
//获取应用实例
const app = getApp()

Page({
  onShareAppMessage: function (res) {
    return {
      title: '太影达人',
      path: './index',
      imageUrl: this.data.userInfo.avatarUrl,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  data: {
    userInfo: {},
    levelName: '',
    levelPercent: '0',
    imgSrc: '@2x'
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (options) {
    // 获取手机屏幕像素比
    this.setData({
      imgSrc: `@2x`
    })
    // try {
    //   var res = wx.getSystemInfoSync()
    //   if (res.pixelRatio > 2) {
    //     app.globalData.imgSrc = `@3x`
    //     this.setData({
    //       imgSrc: `@3x`
    //     })
    //   }
    // } catch (e) {
    //   // Do something when catch error
    // }
    // 设置全局 像素比，加载不同尺寸图片
    // app.globalData.dataInfo = this.data.dataInfo
    // 用户授权
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo()
  },
  getInfo() {
    // 用户授权，获取个人信息
    wx.getUserInfo({
      lang: 'zh_CN',
      success: res => {
        this.getMyUserInfo(res.userInfo)
      },
      fail: res => {
        this.getUserInfoFail()
      }
    })
  },
  getUserInfoFail () {
    wx.showModal({
      title: '警告',
      content: '若不授权微信登录，则无法使用太影达人；点击重新获取授权，则可重新使用；若点击不授权，后期还使用小程序，需在微信【发现】——【小程序】——删掉【太影达人】，重新搜索授权登录方可使用。',
      cancelText: '不授权',
      confirmText: '授权',
      success: (res) => {
        if (res.confirm) {
          wx.openSetting({
            success: (res) => {
              this.getInfo()
            }
          })
        } else if (res.cancel) {
          wx.navigateBack()
        }
      }
    })
  },
  goMysterious () {
    if (this.data.userInfo.score < 100) {
      wx.showModal({
        content: '段位未达到"电影宗师"无法开启',
        showCancel: false
      });
    } else {
      wx.navigateTo({
        url: '../mysterious/mysterious'
      })
    }
  },
  goMyInfo () {
    wx.navigateTo({
      url: '../myInfo/myInfo'
    })
  },
  getMyUserInfo(userInfo) {
    wx.request({
      url: `${app.globalData.baseUrl}/user/${app.globalData.wx_id}`,
      // url: `${app.globalData.baseUrl}/user/test0001`,
      success: (res) => {
        if (Number(res.statusCode) === 200) {
          this.setData({
            userInfo: res.data
          })
          app.globalData.userInfo = res.data
          this.judgeUserInfo()
        } else if (Number(res.statusCode) === 404) {
          // 用户不存在，调注册的接口
          this.register(userInfo)
        }
      },
      fail: (res) => {
        console.log(res)
      }
    })
  },
  register (userInfo) {
    let dataInfo = {
      wx_id: app.globalData.wx_id,
      wx_nickname: userInfo.nickName,
      wx_avatar: userInfo.avatarUrl,
      wx_address: userInfo.city === '' ? userInfo.country : userInfo.city
    }
    wx.request({
      url: `${app.globalData.baseUrl}/user`,
      method: 'POST',
      data: dataInfo,
      success: (res) => {
        this.getMyUserInfo(userInfo)
      },
      fail: (res) => {
      }
    })
  },
  judgeUserInfo () {
    this.setData({
      levelPercent: this.data.userInfo.score < 100 ? this.data.userInfo.score : 100
    })
    let levelNameNow = ''
    if (this.data.userInfo.score < 20) {
      levelNameNow = '群众演员'  
    } else if (20 <= this.data.userInfo.score && this.data.userInfo.score < 45) {
      levelNameNow = '大牌替身'
    } else if (45 <= this.data.userInfo.score && this.data.userInfo.score < 70) {
      levelNameNow = '新晋主角'
    } else if (70 <= this.data.userInfo.score && this.data.userInfo.score < 100) {
      levelNameNow = '超级巨星'
    } else if (100 <= this.data.userInfo.score) {
      levelNameNow = '电影宗师'
    }
    this.setData({
      levelName: levelNameNow
    })
    app.globalData.levelName = levelNameNow
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    if (options.from === 'button') {
      app.globalData.battleId = app.globalData.wx_id + String(new Date().getTime())
      return {
        title: '太影达人',
        path: '/pages/friendWaiting/friendWaiting?battle_id=' + app.globalData.battleId,
        success: function (res) {
          wx.navigateTo({
            url: '../friendWaiting/friendWaiting'
          })
        },
        fail: function (res) {
          // 转发失败
        }
      }
    } else {
      return {
        title: '太影达人',
        path: '/pages/index/index',
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    }
  }
})
